Page({
  data: {
    encryptedData: '',//加密串
    houseInfo: '',//房屋
    url: '',//二维码
    validTime: 0,//有效时间
    CountDown: true,
    islogin: !!getApp().token
  },
  onLoad({ id, encryptedData }) {
    // 判断是业主还是访客
    if (!!id) {
      this.getVisitorCode(id)// 业主获取通行证信息
    } else {
      this.getShareCode(encryptedData)// 访客获取通行证信息
    }

  },
  // 业主获取通行证信息
  async getVisitorCode(id) {
    const res = await wx.http({
      url: `/visitor/${id}`
    })
    let { encryptedData, houseInfo, url, validTime } = res.data
    this.setData({ encryptedData, houseInfo, url, validTime })
  },
  // 访客获取通行证信息
  async getShareCode(encryptedData) {
    const res = await wx.http({
      url: `/visitor/share/${encryptedData}`
    })
    let { houseInfo, url, validTime } = res.data
    this.setData({ houseInfo, url, validTime })
  },

  // 倒计时结束触发
  onTimeCountDown(ev) {
    console.log(ev);
    this.setData({
      CountDown: false
    })
  },
  // 分享触发
  onShareAppMessage() {
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?encryptedData=' + this.data.encryptedData,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
  // 保存图片
  async getImageInfo() {
    // 图片在线地址转为本地临时地址
    const { path } = await wx.getImageInfo({ src: this.data.url })
    // 保存图片
    const res = await wx.saveImageToPhotosAlbum({ filePath: path })
  }
})
